/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.shop;

import com.kmcsoft.comp.ImageUploadComp;
import com.kmcsoft.core.AddressMacro;
import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.emaster.entity.Employee;
import com.kmcsoft.goldshop.model.master.Booth;
import com.kmcsoft.goldshop.model.master.Shop;
import com.kmcsoft.goldshop.shop.service.ShopService;
import com.kmcsoft.util.Utilities;
import com.kmcsoft.utils.i18n;
import com.kmcsoft.zkex.PagingListModel;
import java.util.HashMap;
import java.util.Map;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Tabpanel;
import org.zkoss.zul.Window;

/**
 *
 * @author kmcsoft02
 */
public class ShopComposer extends GenericController<Shop, ShopService> {

    private Booth booth;
    private AddressMacro addr;
    private String blankAvatarPath = "/images/avatar.jpg";
    private Grid boothGrid;
    private Grid editBoothGrid;
    private Grid empGrid;
    private Tabpanel empPanel;
    

    //<editor-fold defaultstate="collapsed" desc="onCreate">
    public void onCreate() {
        onClick$new(null);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="GET/SET">
    public Booth getBooth() {
        return booth;
    }

    public void setBooth(Booth booth) {
        this.booth = booth;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterCompose">
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        booth = new Booth();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getListModel">
    @Override
    public PagingListModel getListModel() {
        String strFilter = "name.matches('(?i).*" + filter.getValue().trim() + ".*')";
        PagingListModel<Shop> model = Utilities.getPagingListModel(objectClass, HashMap.class, "id, createdDate, name, empNumber", strFilter);
        model.setOrdering("createdDate descending");
        model.initialize();
        return model;
    }

    public void setSelected(HashMap selected) {
        super.setSelected(service.getShopById(selected.get("id")));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterNew">
    @Override
    protected void doAfterNew() {
        super.doAfterNew();
        selected.setPicture(blankAvatarPath);
        loadPicture();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doBeforeSave">
    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        if (addr.getText().isEmpty()) {
            throw new WrongValueException(i18n.l("shop.error.address"));
        }
        saveImage();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onSelect$leftList">
    public void onSelect$leftList() {
        loadPicture();
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="uploadImgs">
    private ImageUploadComp imageUpload;

    public void loadPicture() {
        if (selected.getPicture() != null) {
            imageUpload.getImage().setSrc(selected.getPicture());
        } else {
            imageUpload.getImage().setSrc(imageUpload.getBlankAvatarPath());
        }
    }

    public void saveImage() {
        if (selected.getPicture() != null) {
            if (imageUpload.getFileToDelete() != null && imageUpload.getMedia() == null) {
                imageUpload.saveImage();
                selected.setPicture(null);
            } else if (imageUpload.getMedia() != null) {
                imageUpload.saveImage();
                selected.setPicture(imageUpload.getPathFile());
            }
        } else if (imageUpload.getMedia() != null && selected.getPicture() == null) {
            imageUpload.saveImage();
            selected.setPicture(imageUpload.getPathFile());
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="emptab">
    public void onAddEmp() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("component", empPanel);
            map.put("shop", selected);
            map.put("binder", getBinder());
            Window win = (Window) Executions.createComponents("/shop/modal/addEmpModal.zul", null, map);
            win.doModal();
        } catch (Exception ex) {
            log.error("ERROR from onAddEmp", ex);
        }
    }

    public void onRemoveEmp(ForwardEvent event) {
        Employee employee = (Employee) ((Row) event.getOrigin().getTarget().getParent()).getValue();
        selected.removeEmpl(employee);
        service.makePersistent(selected);
        getBinder().loadComponent(leftList);
        getBinder().loadComponent(empGrid);
    }

    @Override
    protected AnnotateDataBinder getBinder() {
        return super.getBinder();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onClick$saveBooth">
    public void onClick$saveBooth() {
        if (booth.getCode().isEmpty()) {
            throw new WrongValueException(i18n.l("shop.booth.error.code.empty"));
        }
        if (checkBoothCode(booth)) {
            throw new WrongValueException(i18n.l("shop.booth.error.code.duplicate"));
        }
        if (booth.getName()==null) {
            throw new WrongValueException(i18n.l("shop.booth.error.name"));
        }
        if (booth.getId() == 0) {
            selected.addBooth(booth);
            service.makePersistent(selected);
        } else {
            service.makePersistent(booth);
        }
        booth = new Booth();
        getBinder().loadComponent(editBoothGrid);
        getBinder().loadComponent(boothGrid);
        getBinder().loadComponent(empGrid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onClick$cancelBooth">
    public void onClick$cancelBooth() {
        booth = new Booth();
        getBinder().loadComponent(editBoothGrid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onViewBooth">
    public void onViewBooth(ForwardEvent event) {
        Booth b = (Booth) ((Row) event.getOrigin().getTarget().getParent()).getValue();
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("booth", b);
            Window win = (Window) Executions.createComponents("/shop/modal/viewBoothModal.zul", null, map);
            win.doModal();
        } catch (Exception ex) {
            log.error("ERROR from onAddEmp", ex);
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onEditBooth">
    public void onEditBooth(ForwardEvent event) {
        booth = (Booth) ((Row) event.getOrigin().getTarget().getParent()).getValue();
        getBinder().loadComponent(editBoothGrid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onDeleteBooth">
    public void onDeleteBooth(ForwardEvent event) {
        Booth b = (Booth) ((Row) event.getOrigin().getTarget().getParent()).getValue();
        selected.removeBooth(b);
        service.deletePersistent(b);
        getBinder().loadComponent(boothGrid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkBoothCode">
    private boolean checkBoothCode(Booth booth) {
        for (Booth b : selected.getBooths()) {
            if (b.getCode().equalsIgnoreCase(booth.getCode())) {
                if (b.getId() != booth.getId()) {
                    return true;
                }
            }
        }
        return false;
    }
    //</editor-fold>
}
